home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / ICON2BIT.ZIP / I2BCONVE.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1996-07-27  |  4.4 KB  |  116 lines

  1. { Copyright ⌐ 1996, Con Brio, Seward, all rights reserved.
  2.  
  3. Although I retain the copyright, this unit and demo are totally
  4. free (along with source code).  You may use the unit i2bConvert
  5. in any program, private or commercial.
  6.  
  7. If you find it useful (or even if you don't) then I would
  8. appreciate any of the following:
  9.  
  10.    * An email message with greetings and where you hail from.
  11.    * Comments, suggestions, bug finds, etc.
  12.    * If you improve the unit, please send me a copy of the
  13.      changes.
  14.    * If you create a component from this code, please send me
  15.      a copy (along with the source, if possible).
  16.  
  17. Mike Reith
  18. Seward, NE, USA
  19. mike@seward.ccsn.edu
  20.  
  21. This unit and the associated demo program can be freely used
  22. and distributed in commercial and private environments, provided
  23. this notice is not modified in any way and there is no charge
  24. for it.
  25.  
  26. Date last modified:  26 July 1996
  27. }
  28.  
  29. unit I2BConvert;
  30.  
  31. interface
  32.  
  33. uses
  34.   Windows, Graphics;
  35.  
  36. const
  37.   i2bDefaultColor : TColor = $00123456;  { pick a dark brown for the
  38.                                            default transparent color }
  39.  
  40. procedure ConvertIcon2Bitmap(Icon : TIcon;
  41.                              TransColor : TColor;
  42.                          var Bitmap : TBitmap);
  43.  
  44. implementation
  45.  
  46. procedure ConvertIcon2Bitmap(Icon : TIcon;
  47.                              TransColor : TColor;
  48.                          var Bitmap : TBitmap);
  49. { Convert the supplied icon into a proper bitmap for Delphi button
  50.   glyphs and return in Bitmap.  Use TransColor for the transparent
  51.   color in the Bitmap.  See the demo program for examples on usage. }
  52. var
  53.   i,j : integer;           { loop control variables }
  54.   IconInfo : TIconInfo;    { information about the icon }
  55.   ANDmask : TBitmap;       { the AND mask of the icon }
  56.   OldMaskColor : TColor;        { keep track of last color in AND mask }
  57.   NewMaskColor : TColor;        { the current color in the AND mask }
  58. begin  { convertIcon2Bitmap }
  59. { create bitmap for the icon AND mask information }
  60. ANDmask := TBitmap.Create;
  61. try
  62.   { Get the icon information and place the AND mask into
  63.     the appropriate TBitmap structure }
  64.   GetIconInfo(Icon.Handle,IconInfo);
  65.   ANDmask.Handle := IconInfo.hbmMask;
  66.   { Set the returning bitmap size and draw the icon onto it }
  67.   Bitmap.Width := ANDmask.Width;
  68.   Bitmap.Height := ANDmask.Height + 1;  { add one line onto the bottom
  69.                                           of the bitmap to keep the
  70.                                           Delphi glyph happy }
  71.   Bitmap.Canvas.Draw(0,0,Icon);
  72.  
  73.   { Where the ANDmask bitmap is transparent, place the transparent color }
  74.   Bitmap.Canvas.Pen.Color := TransColor;
  75.   for j := 0 to ANDmask.Height - 1 do
  76.     begin  { for j }
  77.     OldMaskColor := clRed;  { start off with a color that is not in the mask }
  78.     Bitmap.Canvas.MoveTo(0,j);  { move the the first pixel of this row }
  79.     for i := 0 to ANDmask.Width - 1 do
  80.       begin  { for i }
  81.       NewMaskColor := GetPixel(ANDmask.Canvas.Handle,i,j); { get pixel color }
  82.       if NewMaskColor <> OldMaskColor then
  83.         begin  { NewMaskColor <> OldMaskColor }
  84.         { if the pixel is not the same color as the last pixel then we
  85.           have a trasition -- either from transparent or to transparent
  86.           colors }
  87.         OldMaskColor := NewMaskColor;
  88.         if NewMaskColor = clWhite then
  89.           { transition from solid to transparent -- move to the first
  90.             pixel of the transparent region }
  91.           Bitmap.Canvas.MoveTo(i,j)
  92.         else
  93.           { transition from transparent to solid -- draw a line through
  94.             the transparent region in the transparent color }
  95.           Bitmap.Canvas.LineTo(i,j);
  96.         end;   { NewMaskColor <> OldMaskColor }
  97.       end;  { for i }
  98.     { if the line ends with a transparent color then draw a line through
  99.       the transparent region in the transparent color }
  100.     if NewMaskColor = clWhite then
  101.       Bitmap.Canvas.LineTo(ANDmask.Width,j);
  102.     end;  { for j }
  103.  
  104.   { place an extra line at the bottom of the bitmap so that the
  105.     Delphi TSpeedButton transparent color works correctly }
  106.   Bitmap.Canvas.MoveTo(0,Bitmap.Height - 1);
  107.   Bitmap.Canvas.LineTo(Bitmap.Width,Bitmap.Height - 1);
  108.   finally
  109.     { Release the memory for the icon information }
  110.     ANDmask.Free;
  111.     end;  { try..finally }
  112. end;  { ConvertIcon2Bitmap }
  113.  
  114. end.  { I2BConvert }
  115.  
  116.